/**
 * 前端图片上传、预览
 * 1、利用FileReader,读取blob对象,或者是file对象，将图片转化为data uri的形式。
 * 2、使用canvas,在页面上新建一个画布,利用canvas提供的API,将图片画入这个画布当中。
 * 3、利用canvas.toDataURL()，进行图片的压缩，得到图片的data uri的值
 * 4、上传文件
 */
//图片压缩
function compressImg(img){
	var canvas=document.createElement("canvas");
	var ctx=canvas.getContext("2d");
	var tCanvas=document.createElement("canvas");
	var tctx=tCanvas.getContext("2d");
    var initSize = img.src.length;
    var width = img.width;
    var height = img.height;
    //如果图片大于四百万像素，计算压缩比并将大小压至400万以下
    var ratio;
    if ((ratio = width * height / 4000000)>1) {
        ratio = Math.sqrt(ratio);
        width /= ratio;
        height /= ratio;
    }else {
        ratio = 1;
    }
    canvas.width = width;
    canvas.height = height;
    //铺底色
    ctx.fillStyle = "#fff";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    //如果图片像素大于100万则使用瓦片绘制
    var count;
    if ((count = width * height / 1000000) > 1) {
        count = ~~(Math.sqrt(count)+1); //计算要分成多少块瓦片
		//计算每块瓦片的宽和高
        var nw = ~~(width / count);
        var nh = ~~(height / count);
        tCanvas.width = nw;
        tCanvas.height = nh;
        for (var i = 0; i < count; i++) {
            for (var j = 0; j < count; j++) {
                tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
                ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
            }
        }
    } else {
        ctx.drawImage(img, 0, 0, width, height);
    }
    //进行最小压缩
    var ndata = canvas.toDataURL('image/jpeg', 0.3);
    console.log('压缩前：' + initSize);
    console.log('压缩后：' + ndata.length);
    console.log('压缩率：' + ~~(100 * (initSize - ndata.length) / initSize) + "%");
    tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
    return ndata;
}
//处理iphone拍照图片旋转问题
function getImgData(img,dir,next){
	var image=new Image();
	image.onload=function(){
		var degree=0,drawWidth,drawHeight,width,height;
		drawWidth=this.naturalWidth;
		drawHeight=this.naturalHeight;
		//以下改变一下图片大小
		var maxSide = Math.max(drawWidth, drawHeight);
		if (maxSide > 1024) {
			var minSide = Math.min(drawWidth, drawHeight);
			minSide = minSide / maxSide * 1024;
			maxSide = 1024;
			if (drawWidth > drawHeight) {
				drawWidth = maxSide;
				drawHeight = minSide;
			} else {
				drawWidth = minSide;
				drawHeight = maxSide;
			}
		}
		var canvas=document.createElement('canvas');
		canvas.width=width=drawWidth;
		canvas.height=height=drawHeight;
		var context=canvas.getContext('2d');
		//判断图片方向，重置canvas大小，确定旋转角度，iphone默认的是home键在右方的横屏拍摄方式
		switch(dir){
			//iphone横屏拍摄，此时home键在左侧
			case 3:
			degree=180;
			drawWidth=-width;
			drawHeight=-height;
			break;
			//iphone竖屏拍摄，此时home键在下方(正常拿手机的方向)
			case 6:
			canvas.width=height;
			canvas.height=width;
			degree=90;
			drawWidth=width;
			drawHeight=-height;
			break;
			//iphone竖屏拍摄，此时home键在上方
			case 8:
			canvas.width=height;
			canvas.height=width;
			degree=270;
			drawWidth=-width;
			drawHeight=height;
			break;
		}
		//使用canvas旋转校正
		context.rotate(degree*Math.PI/180);
		context.drawImage(this,0,0,drawWidth,drawHeight);
		//返回校正图片
		next(canvas.toDataURL("image/jpeg",0.8));
	}
	image.src=img;
}